package com.any.sckill.distributed.zookeeper;

import java.util.Objects;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.any.sckill.distributed.zookeeper.impl.CuratorProxyImpl;

public class ZKProxyFactory {
	
	@Autowired(required = false)
	@Qualifier("curatorFramework")
	private CuratorFramework client;
	
	private ZKProxyFactory() {} 
	
	private static final ZKProxyFactory zkProxyFactory = new ZKProxyFactory();
	
	public static CuratorProxy getCuratorProxy(String mutexId) {
		return getCuratorProxy(zkProxyFactory.client, mutexId);
	}
	
	public static CuratorProxy getCuratorProxy(CuratorFramework client, String mutexId) {
		Objects.requireNonNull(client, () -> "Zookeeper proxy factory getCuratorProxy with null client");
		zkProxyFactory.client = client;
		return new CuratorProxyImpl(client).configMutex(mutexId);
	}

}
